import express from 'express'
import api from './api/index.js'
const app = express()
// 设置跨域cors
import cors from 'cors'
import { createConnectionPool } from './mysql/index.js'

const router = express.Router();
app.use(cors())
app.use(express.urlencoded({ extended: true }))
app.use(express.json())

app.use(api)

async function checkMySQLConnection() {
  const pool = await createConnectionPool();
  try {
    // Try executing a simple query, e.g., get the MySQL server version
    const [rows, fields] = await pool.execute('SELECT VERSION() as version');
    console.log('Connected to MySQL. Server version:', rows[0].version);
  } catch (error) {
    console.error('Error connecting to MySQL:', error);
  } finally {
    // Close the connection pool to release resources
    pool.end();
  }
}
checkMySQLConnection();

app.listen(3009, () => {
  console.log(`http://127.0.0.1:3009`)
})
